﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Oridea.Data.Fetching.Tests {
	[TestClass]
	public class OrderingSequenceTests {
		public class Customer {
			public int Id { get; set; }
			public string Name { get; set; }
		}

		private static readonly MockCollection<Customer> Customers = new MockCollection<Customer> {
			new Customer {
				Id = 3,
				Name = "Apple"
			},
			new Customer {
				Id = 2,
				Name = "Apple"
			},
			new Customer {
				Id = 1,
				Name = "Orange"
			},
			new Customer {
				Id = 4,
				Name = "Carrot"
			},
		};

		public static readonly OrderingSequence<Customer> SorterByNameAndId = new OrderingSequence<Customer> {
			customer => customer.Name,
			customer => customer.Id
		};

		[TestMethod]
		public void OrderingSequence_Applies_As_Expected() {
			var fetcher = new CollectionFetcher<Customer>(Customers.AsQueryable());
			IFetcher<ReadOnlyCollection<Customer>> orderedFetcher = fetcher.OrderBy(SorterByNameAndId);
			ReadOnlyCollection<Customer> orderedCustomers = orderedFetcher.Fetch();

			List<Customer> controlList = Customers.OrderBy(customer => customer.Name).
				ThenBy(customer => customer.Id).ToList();
			for (int i = 0; i < controlList.Count; i++) {
				Assert.AreEqual(controlList[i], orderedCustomers[i]);
			}
		}

		[TestMethod]
		public void OrderingSequence_Helps_Paging() {
			var fetcher = new CollectionFetcher<Customer>(Customers.AsQueryable());
			IPagingFetcher<Customer> pagingFetcher = fetcher.OrderBy(SorterByNameAndId);
			ReadOnlyCollection<Customer> secondPage = pagingFetcher.FetchPage(1, 2);

			Assert.AreEqual(secondPage.Count, 2);
			Assert.AreEqual(secondPage[0].Id, 4);
			Assert.AreEqual(secondPage[1].Id, 1);
		}
	}
}
